package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.db.Admin;
import com.tanhua.dubbo.api.db.AdminApi;
import com.tanhua.dubbo.mapper.AdminMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class AdminApiImpl implements AdminApi {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin findByUsername(String username) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Admin::getUsername,username);
        return adminMapper.selectOne(queryWrapper);
    }

    /**
     * 根据微信id查找管理员 霍 2021-7-16
     * @param openid 微信id
     * @return 查找的admin对象
     */
    @Override
    public Admin findByOpenid(String openid) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Admin::getOpenid,openid);
        return adminMapper.selectOne(queryWrapper);
    }

    /**
     * 根据用户id查找管理员 霍 2021-7-16
     * @param userId 用户id
     * @return 查找的admin对象
     */
    @Override
    public Admin findById(Long userId) {
        return adminMapper.selectById(userId);
    }

    @Override
    public void bindWechat(String wechatOpenid, Admin admin) {
        admin.setOpenid(wechatOpenid);
        adminMapper.updateById(admin);
    }

}
